package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DtsGoodsProductServiceImpl implements DtsGoodsProductService {
    @Autowired
    DtsGoodsProductMapper goodsProductMapper;
    @Override
    public Integer getTotal() {
        return goodsProductMapper.selectCount(null);
    }

    @Override
    public void updateProducts(Integer id, DtsGoodsProduct[] goodsproducts) {
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId, id);
        goodsProductMapper.delete(qw);
        if(goodsproducts!= null &&goodsproducts.length > 0){
            for (DtsGoodsProduct product : goodsproducts) {
                product.setUpdateTime(new Date());

                product.setAddTime(new Date());

                product.setGoodsId(id);

                goodsProductMapper.insert(product);
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        DtsGoodsProduct goodsproduct = new DtsGoodsProduct();
        goodsproduct.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId, id);
        goodsProductMapper.update(goodsproduct, qw);
    }

    @Override
    public void addProduct(Integer id, DtsGoodsProduct[] products) {
        if(products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                goodsProductMapper.insert(product);
            }
        }
    }
}
